package com.lifesting.front;

import com.sun.facelets.FaceletContext;
import com.sun.facelets.tag.MetaRule;
import com.sun.facelets.tag.Metadata;
import com.sun.facelets.tag.MetadataTarget;
import com.sun.facelets.tag.TagAttribute;

public class ColumnRule extends MetaRule {
	public static class FieldMetaData extends Metadata{
		private String value;
		
		public FieldMetaData(String value) {
			super();
			this.value = value;
		}

		@Override
		public void applyMetadata(FaceletContext ctx, Object instance) {
			((Column)instance).setField(value);
		}
	}
	public static class SortableMetaData extends Metadata{
		private String value;
		public SortableMetaData(String value) {
			super();
			this.value = value;
		}
		@Override
		public void applyMetadata(FaceletContext ctx, Object instance) {
			((Column)instance).setEnableSorter(Boolean.valueOf(value));	
		}
	}
	public static class FilterableMetaData extends Metadata{
		private String value;

		public FilterableMetaData(String value) {
			super();
			this.value = value;
		}

		@Override
		public void applyMetadata(FaceletContext ctx, Object instance) {
			((Column)instance).setEnableFilter(Boolean.valueOf(value));
		}
		
	}
	public static class WidthMetaData extends Metadata{
		private String value;
		
		public WidthMetaData(String value) {
			super();
			this.value = value;
		}

		@Override
		public void applyMetadata(FaceletContext ctx, Object instance) {
			((IWidthAware)instance).setWidth(Integer.valueOf(value));			
		}
		
	}
	@Override
	public Metadata applyRule(String name, TagAttribute attribute,
			MetadataTarget meta) {
		if (!meta.isTargetInstanceOf(Column.class)) return null;
		if ("field".equals(name))
		{
			return new FieldMetaData(attribute.getValue());
		}
		else if ("sortable".equals(name))
		{
			return new SortableMetaData(attribute.getValue());
		}
		else if ("filterable".equals(name))
		{
			return new FilterableMetaData(attribute.getValue());
		}
		else if ("width".equals(name))
		{
			return new WidthMetaData(attribute.getValue());
		}
		else if ("value".equals(name))
		{
			return new  RnsRule.TextMetadata(attribute.getValue());
		}
		return null;
	}

}
